package net.mihome.app.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.easemob.chatuidemo.activity.ChatActivity;

import net.mihome.app.AppContext;
import net.mihome.app.AppException;
import net.mihome.app.R;
import net.mihome.app.adapter.ChatRoomListAdapter;
import net.mihome.app.bean.ChatRoom;

import java.util.List;

public class ChatRoomListActivity extends Activity {
	private ListView roomlListView;
	private List<ChatRoom> liChatRooms;
	private ChatRoomListAdapter chatRoomListAdapter;
	private AppContext appContext;
	private int begin=0;
	private int num=100;
	private View  progressView;
	private Handler handler;
	private EditText nameEditText;
	private Button addButton;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chat_room_list);
		initView();
	}
	public void initView(){
		roomlListView=(ListView) findViewById(R.id.chat_room_list_listview);
		this.appContext=(AppContext)this.getApplicationContext();
		progressView=findViewById(R.id.news_detail_head_progress);
		nameEditText=(EditText) findViewById(R.id.chat_home_add_name);
		addButton=(Button) findViewById(R.id.btn_add_chat_room);
		addButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
//				addRoom();
			}
		});
		handler=new Handler(new Handler.Callback() {
			@Override
			public boolean handleMessage(Message arg0) {
				// TODO Auto-generated method stub
				switch (arg0.what) {
				case 3:
					nameEditText.setText("");
					break;
				default:
					chatRoomListAdapter = new ChatRoomListAdapter(ChatRoomListActivity.this, liChatRooms,handler,ChatRoom.ALL);
					roomlListView.setAdapter(chatRoomListAdapter);
					chatRoomListAdapter.notifyDataSetChanged();
					progressView.setVisibility(View.GONE);
					break;
				}
				return false;
			}
		});
		roomlListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int arg2,long arg3) {
				ChatRoom chatRoom=(ChatRoom) view.getTag();
				Intent intent=new Intent(ChatRoomListActivity.this,ChatActivity.class);
				intent.putExtra("room_id",chatRoom.getId() );
				intent.putExtra("room_name",chatRoom.getName() );
				startActivity(intent);
			}
		});
		initData();
	}
	/**
	 * 获取房间数据
	 * */
	public void initData(){
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					liChatRooms=appContext.getChatRoomList(begin, num,ChatRoom.ALL);
					handler.sendEmptyMessage(1);
				} catch (AppException e) {
					// TODO Auto-generated catch block
					Toast.makeText(ChatRoomListActivity.this,"网络异常",Toast.LENGTH_SHORT).show();
					e.printStackTrace();
				}
			}
		}).start();
	}
//	/**
//	 * 添加房间
//	 * */
//	public void addRoom(){
//		final String nameString=nameEditText.getText().toString();
//		if(nameString.equals("")){
//			Toast.makeText(this,"房间名称不能为空",Toast.LENGTH_SHORT).show();
//			return;
//		}
//		new Thread(new Runnable() {
//			@Override
//			public void run() {
//				try {
//					ResponsePojo responsePojo=appContext.addChatRoom(nameString);
//					if(responsePojo.getStatus()>0){
//						initData();
//						handler.sendEmptyMessage(3);
//					}else{
//						Toast.makeText(ChatRoomListActivity.this,"添加失败",Toast.LENGTH_SHORT).show();
//					}
//				} catch (AppException e) {
//					// TODO Auto-generated catch block
//					Toast.makeText(ChatRoomListActivity.this,"网络异常",Toast.LENGTH_SHORT).show();
//					e.printStackTrace();
//				}
//			}
//		}).start();
//	}
}
